package com.dianxin.smartpark.service.park.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.constant.ErrMsgEnum;

import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.smartpark.dao.dataobject.park.SmartParkCarParkParking;
import com.dianxin.smartpark.dao.dataobject.park.SmartParkCarParkParkingExample;
import com.dianxin.smartpark.dao.dataobject.visitor.SmartParkVisitor;
import com.dianxin.smartpark.dao.mapper.park.SmartParkCarParkParkingMapper;
import com.dianxin.smartpark.dao.mapper.visitor.SmartParkVisitorMapper;
import com.dianxin.smartpark.dao.qo.park.SmartParkCarParkParkingQO;
import com.dianxin.smartpark.service.park.SmartParkCarParkParkingService;
import com.dianxin.smartpark.service.park.SmartParkCarParkingService;
import com.dianxin.smartpark.service.vo.park.SmartParkCarParkParkingVO;
import com.dianxin.smartpark.service.vo.park.SmartParkCarParkingVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 停车记录业务层处理
 * 
 * @author chenjiasong
 * @date 2021-07-28
 */
@Service
public class SmartParkCarParkParkingServiceImpl implements SmartParkCarParkParkingService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkCarParkParkingServiceImpl.class);
    @Resource
    private SmartParkCarParkParkingMapper smartParkCarParkParkingMapper;

    @Resource
    private SmartParkVisitorMapper smartParkVisitorMapper;

    @Resource
    private SmartParkCarParkingService smartParkCarParkingService;



    /**
     * 查询停车记录
     * 
     * @param qo 停车记录ID
     * @return 停车记录
     */
    @Override
    public CommonResponseBody<SmartParkCarParkParkingVO> selectSmartParkCarParkParking(SmartParkCarParkParkingQO qo)
    {
        if(StringUtils.isNull(qo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkCarParkParkingExample smartParkCarParkParkingExample = new SmartParkCarParkParkingExample();
        SmartParkCarParkParkingExample.Criteria criteria = smartParkCarParkParkingExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        criteria.andIsDeletedEqualTo(0);
        criteria.andIdEqualTo(qo.getId());
        List<SmartParkCarParkParking> passRecords = smartParkCarParkParkingMapper.selectByExample(smartParkCarParkParkingExample);
        return ResponseBodyWrapperUtil.successWrapper(StringUtils.isNotEmpty(passRecords)?passRecords.get(0):null,SmartParkCarParkParkingVO.class);
    }

    /**
     * 查询停车记录列表
     * 
     * @param qo 停车记录
     * @return 停车记录
     */
    @Override
    public CommonResponseBody<List<SmartParkCarParkParkingVO>> selectSmartParkCarParkParkingList(SmartParkCarParkParkingQO qo)
    {
        SmartParkCarParkParkingExample smartParkCarParkParkingExample = new SmartParkCarParkParkingExample();
        SmartParkCarParkParkingExample.Criteria criteria = smartParkCarParkParkingExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        if (qo.getStatus() != null) {
            criteria.andStatusEqualTo(qo.getStatus());
        }
        if (StringUtils.isNotNull(qo.getCarType())) {
            criteria.andCarTypeEqualTo(qo.getCarType());
        }

        if (StringUtils.isNotNull(qo.getStartTime())) {
            smartParkCarParkParkingExample.setStartTime(qo.getStartTime());
        }

        if (StringUtils.isNotNull(qo.getEndTime())) {
            smartParkCarParkParkingExample.setEndTime(qo.getEndTime());
        }
        if (StringUtils.isNotEmpty(qo.getSerachText())) {
            smartParkCarParkParkingExample.setSerachText("%"+qo.getSerachText()+"%");
        }
        criteria.andIsDeletedEqualTo(0);
        if (qo.getNeedPage()) {
            smartParkCarParkParkingExample.setOrderByClause("gmt_create desc limit " + (qo.getPageIndex() - 1) * qo.getPageSize() + "," + qo.getPageSize());
        } else {
            smartParkCarParkParkingExample.setOrderByClause("gmt_create desc");
        }

        List<SmartParkCarParkParking> passRecords = smartParkCarParkParkingMapper.selectByExample(smartParkCarParkParkingExample);
        long count = smartParkCarParkParkingMapper.countByExample(smartParkCarParkParkingExample);
        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, qo, SmartParkCarParkParkingVO.class);
    }

    /**
     * 新增停车记录
     * 
     * @param vo 停车记录
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> insertSmartParkCarParkParking(@Valid SmartParkCarParkParkingVO vo)
    {
        SmartParkCarParkParking smartParkCarParkParking = new SmartParkCarParkParking();
        // 验证 todo
        try {
            BeanUtil.insertConversion(vo, smartParkCarParkParking);
        } catch (Exception e) {
            logger.error("", e);
        }
        //查询访客
        if(vo.getCarType()==2){
            SmartParkVisitor smartParkVisitor = smartParkVisitorMapper.selectByPrimaryKey(vo.getUserId());
            smartParkCarParkParking.setIdNum(smartParkVisitor.getVisitorIdNum());
            smartParkCarParkParking.setUserName(smartParkVisitor.getVisitorName());
            smartParkCarParkParking.setPhoneNum(smartParkVisitor.getVisitorPhoneNum());
        //查询员工
        }else if(vo.getCarType()==1) {
            SmartParkVisitor smartParkVisitor = smartParkVisitorMapper.selectByPrimaryKey(vo.getUserId());
            smartParkCarParkParking.setIdNum(smartParkVisitor.getVisitorIdNum());
            smartParkCarParkParking.setUserName(smartParkVisitor.getVisitorName());
            smartParkCarParkParking.setPhoneNum(smartParkVisitor.getVisitorPhoneNum());
        }
        smartParkCarParkParking.beforeInsert();
        smartParkCarParkParking.setJoinTime(new Date());
        return ResponseBodyWrapperUtil.toResData(smartParkCarParkParkingMapper.insertSelective(smartParkCarParkParking));
    }

    /**
     * 修改停车记录
     * 
     * @param vo 停车记录
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> updateSmartParkCarParkParking(SmartParkCarParkParkingVO vo)
    {
        SmartParkCarParkParkingExample smartParkCarParkParkingExample = new SmartParkCarParkParkingExample();
        SmartParkCarParkParkingExample.Criteria criteria = smartParkCarParkParkingExample.createCriteria();
        criteria.andLicensePlateEqualTo(vo.getLicensePlate());
        List<SmartParkCarParkParking> records = smartParkCarParkParkingMapper.selectByExample(smartParkCarParkParkingExample);
        if(StringUtils.isEmpty(records)){
            return ResponseBodyWrapperUtil.fail("没有入场数据，不能出场","400");
        }
        // 验证 todo
        SmartParkCarParkParking smartParkCarParkParking = records.get(0);
        smartParkCarParkParking.setOutPic(vo.getOutPic());
        smartParkCarParkParking.setOutRoadGate(vo.getOutRoadGate());
        smartParkCarParkParking.setOutTime(new Date());
        smartParkCarParkParking.setStatus(1);
        smartParkCarParkParking.beforeUpdate();
        return ResponseBodyWrapperUtil.toResData(smartParkCarParkParkingMapper.updateByPrimaryKeySelective(smartParkCarParkParking));
    }

    /**
     * 删除停车记录信息
     * 
     * @param vo 停车记录
     * @return 结果08
     */
    @Override
    public CommonResponseBody<Boolean> deleteSmartParkCarParkParking(SmartParkCarParkParkingVO vo)
    {
        // 验证 todo
        if(StringUtils.isNull(vo.getId())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        SmartParkCarParkParking smartParkCarParkParking = new SmartParkCarParkParking();
        try {
            BeanUtil.insertConversion(vo, smartParkCarParkParking);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkCarParkParking.beforeDelete();
        return ResponseBodyWrapperUtil.toResData(smartParkCarParkParkingMapper.updateByPrimaryKeySelective(smartParkCarParkParking));
    }

    /**
     * 新增停车记录
     *
     * @param vo 停车记录
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> saveSmartParkCarParking(@Valid SmartParkCarParkingVO vo) throws Exception {
        // 验证 todo
        if(StringUtils.isNull(vo.getRoadGate())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        if(StringUtils.isNull(vo.getOpenType())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        if(StringUtils.isNull(vo.getRoadGate())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        if(StringUtils.isNull(vo.getLicensePlate())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        if(StringUtils.isNull(vo.getCameraDeviceCode())){
            return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
        }
        logger.debug("!!!!!!!!!!!!!start!!!!!!!!!!!!!!!!!!!!!");
        //异步去处理  直接返回
        smartParkCarParkingService.insertSmartParkCarParking(vo);
        logger.debug("!!!!!!!!!!!!!end!!!!!!!!!!!!!!!!!!!!!");
        return ResponseBodyWrapperUtil.success();
    }
}
